package com.zyx.nettypro.netty.heartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

import java.time.LocalDateTime;

/**
 * @author Yaxi.Zhang
 * @since 2021/9/23 13:47
 */
public class MyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        // 向下转型
        IdleStateEvent event = (IdleStateEvent) evt;

        String eventType = null;
        switch (event.state()) {
            case READER_IDLE:
                eventType = "read idle " + LocalDateTime.now();
                break;
            case WRITER_IDLE:
                eventType = "writer idle " + LocalDateTime.now();
                break;
            case ALL_IDLE:
                eventType = "all idle " + LocalDateTime.now();
                break;
            default:
                eventType = "unknown idle~" + LocalDateTime.now();
        }

        System.out.println(ctx.channel().remoteAddress() + " =====>>>>> " + eventType);

    }
}
